/**
 * 
 */
package edu.uc.ui;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;

import com.foodview1.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * mapRestaurantActivity class
 * 
 * @author Ckorczyk1
 * 
 * This class controls the mapview.xml page to display google maps
 * 
 */
public class mapRestaurantActivity extends MapActivity implements
		LocationListener {

	int lat;
	int lon;
	long start;
	long stop;
	LocationManager locationManager;
	String provider;
	int x;
	int y;
	private MapView mapView;
	private Drawable drawable;
	private List<Overlay> mapOverlays;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(com.foodview1.R.layout.map_restaurants);

		mapView = (MapView) findViewById(com.foodview1.R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		Marker marker = new Marker();

		mapOverlays = mapView.getOverlays();
		mapOverlays.add(marker); // add the markers to the overlay list

		drawable = this.getResources().getDrawable(
				com.foodview1.R.drawable.androidmarker);
		@SuppressWarnings("unused")
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
				drawable, this);

		// Get user location
		// placing pinpoint at location
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria crit = new Criteria();
		
		provider = locationManager.getBestProvider(crit, false);
		Location location = locationManager.getLastKnownLocation(provider);

		
		if (location != null) {
			lat = (int) (location.getLatitude()  *1E6);
			lon = (int) (location.getLongitude()  *1E6);

			// create a GeoPoint that defines the map coordinates for the first
			// overlay item, and pass it to a new {@code OverlayItem}
			GeoPoint ourLocation = new GeoPoint(lat, lon);
			OverlayItem overlayItem = new OverlayItem(ourLocation, "String 1",
					"String 2");
			CustomPinpoint custom = new CustomPinpoint(drawable,
					mapRestaurantActivity.this);
			custom.insertPinpoint(overlayItem);
			mapOverlays.add(custom);
			
		} else {
			// toast
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	public void onStart() {
		super.onStart();
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	public void onResume() {
		super.onResume();
		// TODO Auto-generated method stub
		locationManager.requestLocationUpdates(provider, 500, 1, this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	public void onPause() {
		super.onPause();
		// TODO Auto-generated method stub
		locationManager.removeUpdates(this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	public void onStop() {
		super.onStop();
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		lat = (int) (location.getLatitude() *1E6);
		lon = (int) (location.getLongitude()  *1E6);
		GeoPoint ourLocation = new GeoPoint(lat, lon);
		OverlayItem overlayItem = new OverlayItem(ourLocation, "String 1",
				"String 2");
		CustomPinpoint custom = new CustomPinpoint(drawable,
				mapRestaurantActivity.this);
		custom.insertPinpoint(overlayItem);
		mapOverlays.add(custom);

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	class Marker extends Overlay {
		private GeoPoint touchedPoint;

		/*
		 * this ontouch event would be if we wanted to add pin points to our
		 * map. If the user holds down for a period of time it would add one
		 */
		@SuppressWarnings("deprecation")
		public boolean onTouchEvent(MotionEvent e, MapView m) {
			if (e.getAction() == MotionEvent.ACTION_DOWN) {
				start = e.getEventTime();
				x = (int) e.getX();
				y = (int) e.getY();
				touchedPoint = mapView.getProjection().fromPixels(x, y);

			}
			if (e.getAction() == MotionEvent.ACTION_UP) {
				stop = e.getEventTime();
			}
			if (stop - start > 1500) {
				AlertDialog alert = new AlertDialog.Builder(null).create();
				alert.setTitle("Pick Option");
				alert.setMessage("Pick an option.");
				alert.setButton("Place a pinpoint",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

								OverlayItem overlayItem = new OverlayItem(
										touchedPoint, "String 1", "String 2");
								CustomPinpoint custom = new CustomPinpoint(
										drawable, mapRestaurantActivity.this);
								custom.insertPinpoint(overlayItem);
								mapOverlays.add(custom);

							}
						});
				alert.setButton2("Get Location",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

								Geocoder geocoder = new Geocoder(
										getBaseContext(), Locale.getDefault());
								try {
									List<Address> address = geocoder.getFromLocation(
											touchedPoint.getLatitudeE6() / 1E6,
											touchedPoint.getLongitudeE6() / 1E6,
											1);
									if (address.size() > 0) {
										for (int i = 0; i < address.get(0)
												.getMaxAddressLineIndex(); i++) {
											@SuppressWarnings("unused")
											String display = "";
											display += address.get(0)
													.getAddressLine(i) + "\n";
										}
									}
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} finally {

								}
							}
						});
			}

			return false;

		}
	}

	//variables for menu items
		public static final int SEARCH_RESTAURANTS = 1;
		public static final int REVIEW_MEAL = 2;
		public static final int NEARBY_RESTAURANTS = 3;
		public static final int SHOW_MAP = 4;
		public static final int MY_REVIEWS = 5;

		/**
		 * onCreateMethod
		 */
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			getMenuInflater().inflate(R.menu.activity_main, menu);
			
//			menu.add(Menu.NONE, SEARCH_RESTAURANTS, Menu.FIRST, "Search");
//			menu.add(Menu.NONE, REVIEW_MEAL, Menu.FIRST+1, "Review Meal");
//			menu.add(Menu.NONE, NEARBY_RESTAURANTS, Menu.FIRST+2, "Nearby Restaurants");
//			menu.add(Menu.NONE, SHOW_MAP, Menu.FIRST+3, "Show Map");
			return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			super.onOptionsItemSelected(item);
			
			switch (item.getItemId()) {
			case R.id.SEARCH_RESTAURANTS:
				searchRestaurants();
				break;
			case R.id.SHOW_MAP:
				showMap();
				break;
			case R.id.MY_REVIEWS:
				myReviews();
				break;
			case R.id.MAIN:
				home();
				break;
			case R.id.REVIEW_FOOD:
				reviewFood();
				break;
			default:
				// this is what happens if there is not a case that matches item.getItemId().
			}
			
			return true;
			
			
		}
		// method showMap - brings up the map screen to display nearby restaurants
		private void showMap() {
			Intent showMapRestaurants = new Intent(this, mapRestaurantActivity.class);
			startActivity(showMapRestaurants);
			
		}
		// method searchRestaurants - Brings up the search screen containing search criteria
		private void searchRestaurants() {
			Intent searchRestaurants = new Intent(this, SearchActivity.class);
			startActivity(searchRestaurants);
			
		}
		//method myReviews - brings up the screen with a list of all of the users previous reviews
		private void myReviews() {
			Intent myReviews = new Intent(this, myReviewsActivity.class);
			startActivity(myReviews);
		}

		// method home - returns the user to the home screen
		private void home() {
			Intent home = new Intent(this, MainActivity.class);
			startActivity(home);
		}
		// method reviewFood - new review screen
		private void reviewFood() {
			Intent reviewFood = new Intent(this, ReviewFoodActivity.class);
			startActivity(reviewFood);
		}
	
}
